package com.pan.kang.builder;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pan.kang.model.TableSchemaExcel;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class ExcelBuilder {

    public static List<TableSchemaExcel> buildFromExcel(MultipartFile file) throws IOException {
        return upload(file, TableSchemaExcel.class);
    }


    public static <T> List<T> upload(MultipartFile file, Class<T> clazz) throws IOException {
        List<T> list = new LinkedList<>();
        EasyExcel.read(file.getInputStream(), clazz, new AnalysisEventListener<T>() {
            @Override
            public void invoke(T data, AnalysisContext analysisContext) {
                list.add(data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            }
        }).sheet().doReadSync();
        return list;
    }
}
